/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleship_server;

import battleship_server.threads.GameLogicThread;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author omar
 */
public class Manager {

    // Todos los jugadores, estén jugando o no
    public static LinkedList<SocketConnection> onlineUsers = new LinkedList<SocketConnection>();
    // El arreglo de jugadores esperando
    public static LinkedList<SocketConnection> queuedPlayers = new LinkedList<SocketConnection>();
    // El arreglo de juegos.
    public static ArrayList<GameLogicThread> games = new ArrayList<GameLogicThread>();
    // El arreglo de id's únicos para cada conexión.
    public static ArrayList<Integer> uniqueIDs = new ArrayList<Integer>();


    public static int activeConnections() {
        return onlineUsers.size() + games.size() * 2;
    }

    public static int activeGames() {
        return games.size();
    }

    public static int allPlayers() {
        return onlineUsers.size();
    }

    public static String giveMeGameInfo(int i) {
        GameLogicThread glt = games.get(i);
        String players = "";

        String hundidosPorElUno = Arrays.toString(glt.getHundidosdos());
        String hundidosPorElDos = Arrays.toString(glt.getHundidosuno());

        String res = "Info for game " + i + ".\n\nPlayers are:\n " + players + "\n\nGame state:\nPlayer 1 has sunk: " + hundidosPorElUno + "\nPlayer 2 has sunk:" + hundidosPorElDos + ".\n";
        return res;
    }
    
    public synchronized static ArrayList getOnlineListUsers() {
        ArrayList userNames = new ArrayList();
        Iterator iterator = onlineUsers.listIterator();
        while (iterator.hasNext()) {
            SocketConnection sc = (SocketConnection)iterator.next();
            userNames.add(sc.getPlayer().getName());
        }
        return userNames;
    }
}
